using System;
using System.Collections.Generic;
using System.Text;
using Barrage.Global;
using Barrage.Global.Vertex;
using SlimDX.Direct3D9;
using SlimDX;

namespace Barrage.Meshes
{
    /// <summary>
    /// Renders a standard Teapot mesh
    /// </summary>
    public class Teapot : Barrage.Mesh
    {
        /// <summary>
        /// The teapot mesh is shared between all instances of the class since it won't be modified in any way
        /// </summary>
        private static SlimDX.Direct3D9.Mesh teapotMesh = null;

        private static void initializeBuffer()
        {
            teapotMesh = SlimDX.Direct3D9.Mesh.CreateTeapot(Engine.Device);
        }

        /// <summary>
        /// Constructor. Creates the teapot if necessary
        /// </summary>
        public Teapot()
        {
            if (teapotMesh == null)
                initializeBuffer();
            materials.Add(new Material());
        }

        /// <summary>
        /// \internal Renders the teapot
        /// </summary>
        /// <param name="item">The item that's being rendered</param>
        public override void Render(Item item)
        {
            shader.BeginRender(item, materials[0]);
            int passNumber = shader.PassNumber(materials[0]);
            for (int j = 0; j < passNumber; j++)
            {
                shader.BeginPass(j);
                teapotMesh.DrawSubset(0);
                shader.EndPass();
            }
            
            shader.EndRender();

            NLog.LogManager.GetLogger("Barrage::Scene::Graphics3D::Teapot").Debug("Rendering Teapot");
        }
    }
}
